package com.example.hotel;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import com.example.hotel.Add_cart.AdapterListViewData;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.StrictMode;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class Check_out extends Activity implements OnClickListener {
	public ArrayList<String> message = new ArrayList<String>();
	HashMap<String, String> planetList1;
	SimpleAdapter sAdap;
	ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
	TextView history, date, wellcome, tv_total_price, tv_Product_name, tv_Type,
			tv_Price, tv_QTY, tv_Total, tv_Update, tv_Remove, tv_Total_Payment,
			tv_Date, tv_label;

	ImageView back_home, image_cart, image_msg, image_bill;
	final ArrayList<String> date_time = new ArrayList<String>();
	final ArrayList<String> product_name = new ArrayList<String>();
	final ArrayList<String> type_product = new ArrayList<String>();
	final ArrayList<String> price_product = new ArrayList<String>();
	final ArrayList<String> qty_product = new ArrayList<String>();
	final ArrayList<String> total = new ArrayList<String>();
	Double total_price = 0.00;
	Boolean flage = true;
	ArrayList<HashMap<String, String>> myArrList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.check_out);
		if (android.os.Build.VERSION.SDK_INT > 9) {
			StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
					.permitAll().build();
			StrictMode.setThreadPolicy(policy);
		}
		getCheck_out();
		TextView tvAmount = (TextView) findViewById(R.id.tvAmount);
		tvAmount.setText("("
				+ ((Global) getApplicationContext()).cart_count.toString()
				+ ")");
		myArrList = new ArrayList<HashMap<String, String>>();
		// tv_total_price = (TextView) findViewById(R.id.tv_total_price);
		tv_Total_Payment = (TextView) findViewById(R.id.tv_Total_Payment);
		tv_Product_name = (TextView) findViewById(R.id.tv_Product_name);
		tv_Type = (TextView) findViewById(R.id.tv_Type);
		tv_Price = (TextView) findViewById(R.id.tv_Price);
		tv_QTY = (TextView) findViewById(R.id.tv_QTY);
		tv_Date = (TextView) findViewById(R.id.tv_Date);
		tv_Total = (TextView) findViewById(R.id.tv_Total);
		tv_label = (TextView) findViewById(R.id.textView7);
		back_home = (ImageView) findViewById(R.id.imagehome);
		image_cart = (ImageView) findViewById(R.id.image_cart);
		image_msg = (ImageView) findViewById(R.id.image_msg);
		image_bill = (ImageView) findViewById(R.id.image_bill);
		image_msg.setOnClickListener(this);
		image_cart.setOnClickListener(this);
		image_bill.setOnClickListener(this);
		back_home.setOnClickListener(this);
		wellcome = (TextView) findViewById(R.id.tvWellcome);
		wellcome.setText(((Global) getApplicationContext()).wellcome);

		if (flage) {
			DecimalFormat decimalFormat = new DecimalFormat("###,##0.00");
			tv_Total_Payment.setText(decimalFormat.format(total_price)
					+ " TH(BATH)");
			HashMap<String, String> map;
			for (int i = 0; i < date_time.size(); i++) {
				map = new HashMap<String, String>();
				map.put("Date", date_time.get(i));
				map.put("product_name", product_name.get(i));
				map.put("type_product", type_product.get(i));
				map.put("price_product", price_product.get(i));
				map.put("qty_product", qty_product.get(i));
				map.put("total", total.get(i));
				myArrList.add(map);
			}
			ListView listViewData = (ListView) findViewById(R.id.listView1);
			
			/*---------SetCustomList-------*/
			ListAdapter listAdapter;
			listAdapter = new CustomListAdapter(Check_out.this,
					R.layout.check_out_column, date_time, product_name,
					type_product, price_product, qty_product, total);
			listViewData.setAdapter(listAdapter);
		}

		/*------setfonts------*/

		Typeface Font = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-Kittithada.ttf");
		Typeface FontBold = Typeface.createFromAsset(getAssets(),
				"fonts/PSL-KittithadaBold.ttf");
		TextView tv_amount = (TextView) findViewById(R.id.tvAmount);
		tv_amount.setTypeface(Font);
		wellcome.setTypeface(FontBold);
		tv_Date.setTypeface(FontBold);
		tv_Product_name.setTypeface(FontBold);
		tv_Price.setTypeface(FontBold);
		tv_Type.setTypeface(FontBold);
		tv_QTY.setTypeface(FontBold);
		tv_Total.setTypeface(FontBold);
		// tv_total_price.setTypeface(FontBold);
		tv_Total_Payment.setTypeface(FontBold);
		tv_label.setTypeface(FontBold);
		
		
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.imagehome:
			startActivity(new Intent(Check_out.this, home.class));
			break;		
		case R.id.image_cart:		 
			((Global) getApplicationContext()).nameCart = "show_cart";
			startActivity(new Intent(Check_out.this, Add_cart.class));
			break;
		case R.id.image_msg:
			startActivity(new Intent(Check_out.this, Message_list.class));
			break;
		case R.id.image_bill:
			startActivity(new Intent(Check_out.this, Billing_history.class));
			break;
		}
	}

	// /CustomList setFontListView
	@SuppressWarnings("rawtypes")
	private class CustomListAdapter extends ArrayAdapter {

		private Context mContext;
		private int id;
		private List<String> Ldate;
		private List<String> Lproduct_name;
		private List<String> Ltype_product;
		private List<String> Lprice_product;
		private List<String> Lqty;
		private List<String> Ltotal;

		@SuppressWarnings("unchecked")
		public CustomListAdapter(Context context, int textViewResourceId,
				ArrayList<String> Date, ArrayList<String> product_name,
				ArrayList<String> type_product,
				ArrayList<String> price_product, ArrayList<String> qty,
				ArrayList<String> total) {
			super(context, textViewResourceId, Date);
			mContext = context;
			id = textViewResourceId;
			Ldate = Date;
			Lproduct_name = product_name;
			Ltype_product = type_product;
			Lprice_product = price_product;
			Lqty = qty;
			Ltotal = total;
		}

		@Override
		public View getView(int position, View v, ViewGroup parent) {
			View mView = v;
			if (mView == null) {
				LayoutInflater vi = (LayoutInflater) mContext
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				mView = vi.inflate(id, null);
			}
			TextView date1 = (TextView) mView.findViewById(R.id.date);
			TextView prodruct_name1 = (TextView) mView
					.findViewById(R.id.prodruct_name);
			TextView type_product1 = (TextView) mView
					.findViewById(R.id.type_product);
			TextView type_name1 = (TextView) mView.findViewById(R.id.price);
			TextView qty1 = (TextView) mView.findViewById(R.id.qty);
			TextView total1 = (TextView) mView.findViewById(R.id.total);
			if ((Ldate.get(position) != null)
					&& (Lproduct_name.get(position) != null)
					&& (Ltype_product.get(position) != null)
					&& (Lprice_product.get(position) != null)
					&& (Lqty.get(position) != null)
					&& (Ltotal.get(position) != null)) {
				Typeface Font = Typeface.createFromAsset(getAssets(),
						"fonts/PSL-Kittithada.ttf");
				date1.setTypeface(Font);
				date1.setText(Ldate.get(position));
				prodruct_name1.setTypeface(Font);
				prodruct_name1.setText(Lproduct_name.get(position));
				type_product1.setTypeface(Font);
				type_product1.setText(Ltype_product.get(position));
				type_name1.setTypeface(Font);
				type_name1.setText(Lprice_product.get(position));
				qty1.setTypeface(Font);
				qty1.setText(Lqty.get(position));
				total1.setTypeface(Font);
				total1.setText(Ltotal.get(position));
			}
			return mView;
		}

	}

	public void getCheck_out() {
		String URL1 = ((Global) getApplicationContext()).url;
		String SOAP_ACTION = ((Global) getApplicationContext()).soap_acction
				+ "check_out";
		String NAMESPACE = "";
		String METHOD_NAME = "check_out";
		String lang = ((Global) getApplicationContext()).lang;
		String imei = ((Global) getApplicationContext()).imei;
		SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
		SoapObject responses = null;
		request.addProperty("id", imei);
		request.addProperty("lang", lang);
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE androidHttpTransport = new HttpTransportSE(URL1);
		androidHttpTransport.debug = true;

		try {
			androidHttpTransport.call(SOAP_ACTION, envelope);
			responses = (SoapObject) envelope.bodyIn;
			String str = responses.toString();
			int indexSub = str.indexOf("=");
			String b = str.substring(indexSub + 1);
			indexSub = b.indexOf(";");
			b = b.substring(0, indexSub);
			String[] a = b.split("\\|");
			if (a[0].isEmpty()) {
				flage = false;
			} else {
				flage = true;
				for (int i = 0; i < a.length; i++) {
					String[] tmp = a[i].split("\\*");
					date_time.add(tmp[0]);
					product_name.add(tmp[1]);
					type_product.add(tmp[2]);
					price_product.add(tmp[3]);
					qty_product.add(tmp[4]);
					Double p = Double.parseDouble(tmp[5]);
					total_price += p;
					DecimalFormat decimalFormat = new DecimalFormat(
							"###,##0.00");
					total.add(decimalFormat.format(p));

				}
			}

		} catch (Exception e) {
			// Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();

		}
	}

}
